<?php

namespace App\Console\Commands\Elasticsearch\Indices;

use Illuminate\Support\Facades\Artisan;
class ProjectIndex
{
    public static function getAliasName()
    {
        return 'movies';
    }

    public static function getProperties()
    {
        return [
            'name'         => ['type' => 'text', 'analyzer' => 'ik_smart', 'search_analyzer' => 'ik_smart_synonym'],
            'short_name'   => ['type' => 'keyword'],
            'outher_name'    => ['type' => 'text', 'analyzer' => 'ik_smart', 'search_analyzer' => 'ik_smart_synonym'],
            'introduction'   => ['type' => 'text', 'analyzer' => 'ik_smart', 'search_analyzer' => 'ik_smart_synonym'],
            'score'         => ['type' => 'scaled_float', 'scaling_factor' => 10],
            'score_people'  => ['type' => 'integer'],
            'year'          => ['type' => 'integer'],
            'scores'        => ['type' => 'integer'],
            'areas'         => [
                'type'      => 'nested',
                'properties' => [
                    'name'       => [
                        'type'            => 'keyword',
                        'copy_to'         => 'area_name',
                    ],
                    'id' => [
                        'type'     => 'integer',
                        'copy_to'  => 'area_id',
                    ],
                ],
            ],
            'categories'     => [
                'type'       => 'nested',
                'properties' => [
                    'name'       => [
                        'type'            => 'keyword',
                        'copy_to'         => 'category_name',
                    ],
                    'id' => [
                        'type'     => 'integer',
                        'copy_to'  => 'category_id',
                    ],
                ],
            ],
            'actors' => [
                'type' => 'nested',
                'properties' => [
                    'name'       => [
                        'type'            => 'text',
                        'analyzer'        => 'ik_smart',
                        'search_analyzer' => 'ik_smart_synonym',
                        'copy_to'         => 'actor_name',
                    ],
                    'id' => [
                        'type'     => 'integer',
                        'copy_to'  => 'actor_id',
                    ],
                ],
            ],
            'directors' => [
                'type' => 'nested',
                'properties' => [
                    'name'       => [
                        'type'            => 'text',
                        'analyzer'        => 'ik_smart',
                        'search_analyzer' => 'ik_smart_synonym',
                        'copy_to'         => 'director_name',
                    ],
                    'id' => [
                        'type'     => 'integer',
                        'copy_to'  => 'director_id',
                    ],
                ],
            ],
            'authors' => [
                'type' => 'nested',
                'properties' => [
                    'name'       => [
                        'type'            => 'text',
                        'analyzer'        => 'ik_smart',
                        'search_analyzer' => 'ik_smart_synonym',
                        'copy_to'         => 'author_name',
                    ],
                    'id' => [
                        'type'     => 'integer',
                        'copy_to'  => 'author_id',
                    ],
                ],
            ],
        ];
    }

    public static function getSettings()
    {
        return [
            'analysis' => [
                'analyzer' => [
                    'ik_smart_synonym' => [
                        'type'      => 'custom',
                        'tokenizer' => 'ik_smart',
                        'filter'    => ['synonym_filter'],
                    ],
                ],
                'filter'   => [
                    'synonym_filter' => [
                        'type'          => 'synonym',
                        'synonyms_path' => 'analysis/synonyms.txt',
                    ],
                ],
            ],
        ];
    }

    public static function rebuild($indexName)
    {
        // 通过 Artisan 类的 call 方法可以直接调用命令
        // call 方法的第二个参数可以用数组的方式给命令传递参数
        Artisan::call('es:sync-movies', ['--index' => $indexName]);
    }
}
